@file:Suppress("SpellCheckingInspection")

package han.cirno.corrupt.components.school.impl.fafu

import han.cirno.corrupt.components.school.SchoolImpl
import han.cirno.corrupt.components.school.SchoolProvider
import han.cirno.corrupt.components.school.impl.CommonNetClient
import han.cirno.corrupt.data.instances.UserSchoolData
import okhttp3.HttpUrl

object FafuEduConst {
    const val HOST="jwgl.fafu.edu.cn"
    const val ROOT_ADDRESS: String = "http://$HOST"
    fun isLoopBacked(httpUrl: HttpUrl):Boolean= httpUrl.toString().run {
            contains("default2.aspx")||contains("zdy.htm")||endsWith(")/")}

    fun getMainPageAddress():String{
        val operation = getDataOperation()
        return "$ROOT_ADDRESS/${operation.singleGet(SchoolProvider.KEY_TOKEN,String::class)}/xs_main.aspx?xh=${operation.singleGet(
            SchoolProvider.KEY_ID,String::class)}"
    }

    private fun getDataOperation(): SchoolProvider.Editor{
        return SchoolImpl.getImpl(UserSchoolData.School.FAFU)!!.dataOperation()
    }

    sealed class Payload(
        path:String,
        queryPairs:Array<String>,
        postForm:Array<String>?=null,
        headers:Array<String>?=null):
        CommonNetClient.Payload("http", HOST,
            "/${getDataOperation().singleGet(SchoolProvider.KEY_TOKEN,String::class)}/$path",
            queryPairs, postForm, headers){
        companion object{
            const val VIEW_STATE="__VIEWSTATE"
            const val EVENT_TARGET="__EVENTTARGET"
            const val EVENT_TARGUMENT="__EVENTARGUMENT"
            const val FUNCTION_MODULE_CODE="gnmkdm"
        }
        data object MainPage: Payload(
            "xs_main.aspx",
            arrayOf("xh", getDataOperation().singleGet(SchoolProvider.KEY_ID,String::class)!!))
        data class ClassesPage(val viewState:String?=null, val xnd:String?=null, val xqd:String?=null, val target:String?=null):
            Payload(
            "xskbcx.aspx",
            arrayOf(
                FUNCTION_MODULE_CODE,"N121603",
                "xh", getDataOperation().singleGet(SchoolProvider.KEY_ID,String::class)!!,
                "xm", getDataOperation().singleGet(SchoolProvider.KEY_USERNAME,String::class)!!),
            if (viewState == null||xnd==null||xqd==null||target==null) null
            else
                arrayOf(
                    VIEW_STATE,viewState,
                    EVENT_TARGET,target,
                    EVENT_TARGUMENT,"",
                    "xnd",xnd,
                    "xqd",xqd),
            arrayOf("Referer", getMainPageAddress())
        )
        data class ExamPage(val viewState:String?=null,val xnd:String?=null,val xqd:String?=null):
            Payload(
                "xskscx.aspx",
                arrayOf(
                    FUNCTION_MODULE_CODE,"N121604",
                    "xh", getDataOperation().singleGet(SchoolProvider.KEY_ID,String::class)!!,
                    "xm", getDataOperation().singleGet(SchoolProvider.KEY_USERNAME,String::class)!!),
                if (viewState == null||xnd==null||xqd==null) null
                else
                    arrayOf(
                        VIEW_STATE,viewState,
                        //EVENT_TARGET,"xqd",
                        EVENT_TARGUMENT,"",
                        "xnd",xnd,
                        "xqd",xqd),
                arrayOf("Referer", getMainPageAddress())
            )
        data class ScorePage(val viewState: String?=null,val ddlxn:String?=null,val ddlxq:String?=null):
            Payload(
                "xscjcx_dq_fafu.aspx",
                arrayOf(
                    FUNCTION_MODULE_CODE,"N121605",
                    "xh", getDataOperation().singleGet(SchoolProvider.KEY_ID,String::class)!!,
                    "xm", getDataOperation().singleGet(SchoolProvider.KEY_USERNAME,String::class)!!
                ),
                if (viewState==null||ddlxn==null||ddlxq==null)null
                else
                    arrayOf(
                        VIEW_STATE,viewState,
                        EVENT_TARGUMENT,"",
                        "ddlxn",ddlxn,
                        "ddlxq",ddlxq),
                arrayOf("Referer", getMainPageAddress())
            )
    }
}